package com.cloud.job.handler;

import com.alibaba.fastjson.JSON;
import com.cloud.entity.ExceptionLogEntity;
import com.cloud.entity.mes.MesQmMaintenanceDtl;
import com.cloud.mapper.MesQmMaintenanceDtlMapper;
import com.cloud.service.ExceptionLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class MesQmMaintenanceDtlJobHandler {
    @Autowired
    private MesQmMaintenanceDtlMapper mesQmMaintenanceDtlMapper;
    @Autowired
    private ExceptionLogService exceptionLogService;

    public void execute(ExceptionLogEntity exceptionLogEntity) {
        MesQmMaintenanceDtl mesQmMaintenanceDtl = JSON.parseObject(exceptionLogEntity.getBody(), MesQmMaintenanceDtl.class);
        if ("insert".equals(exceptionLogEntity.getType())) {
            try {
                Integer integer = mesQmMaintenanceDtlMapper.insertMesQmMaintenanceDtl(mesQmMaintenanceDtl);
                if (integer < 1) {
                    updateExceptionLog(exceptionLogEntity);
                }
                // 处理成功  删除
                else {
                    deleteExceptionLog(exceptionLogEntity);
                }
            } catch (Exception e) {
                e.printStackTrace();
                updateExceptionLog(exceptionLogEntity);
            }
        }
        if ("update".equals(exceptionLogEntity.getType())) {
            try {
                Integer integer = mesQmMaintenanceDtlMapper.updateMesQmMaintenanceDtl(mesQmMaintenanceDtl);
                if (integer < 1) {
                    //更新状态下两种异常数据
                    //①乐观锁没更新的数据（由于后更新的已经更新成功，所以这些数据需要删除）
                    //②先更新，但插入还没插入结束（该数据需要保留）
                    MesQmMaintenanceDtl mesQmMaintenanceDtlEntity = mesQmMaintenanceDtlMapper.selectMesQmMaintenanceDtl(mesQmMaintenanceDtl);
                    // 不等于空，代表这个数据是无用的则删除
                    if (mesQmMaintenanceDtlEntity != null) {
                        deleteExceptionLog(exceptionLogEntity);
                    }
                    //等于空,代表还没插入成功 就做更新，该数据存活可以控制存活次数
                    else {
                        updateExceptionLog(exceptionLogEntity);
                    }
                }
                // 处理成功  删除
                else {
                    deleteExceptionLog(exceptionLogEntity);
                }
            } catch (Exception e) {
                e.printStackTrace();
                updateExceptionLog(exceptionLogEntity);
            }
        }
        if ("delete".equals(exceptionLogEntity.getType())) {
            try {
                Integer integer = mesQmMaintenanceDtlMapper.deleteMesQmMaintenanceDtl(mesQmMaintenanceDtl);
                if (integer < 1) {
                    updateExceptionLog(exceptionLogEntity);
                }
                // 处理成功  删除
                else {
                    deleteExceptionLog(exceptionLogEntity);
                }
            } catch (Exception e) {
                e.printStackTrace();
                updateExceptionLog(exceptionLogEntity);
            }
        }
    }

    private void updateExceptionLog(ExceptionLogEntity exceptionLogEntity) {
        try {
            //更新异常表
            exceptionLogEntity.setRetry(exceptionLogEntity.getRetry() + 1);
            exceptionLogEntity.setUpdTime(new Date());
            exceptionLogService.updateExceptionLogById(exceptionLogEntity);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("更新exception_log失败：" + e);
        }
    }

    private void deleteExceptionLog(ExceptionLogEntity exceptionLogEntity) {
        try {
            //删除异常表
            exceptionLogService.deleteById(exceptionLogEntity);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除exception_log失败：" + e);
        }
    }
}
